<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\Admin\Menu;
use App\Models\Admin\Role;
class MenuController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $model = Menu::all();
        $data = getTree($model);
        return view('admin.admin_menu.index',compact('data'));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $model = Menu::all();
        $data = getTree($model);
        return view('admin.admin_menu.add',compact('data'));

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        Menu::create($request->except('_token'));
        return ['status'=>200,'msg'=>'成功'];

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $list = Menu::all();
        $menuList = getTree($list);
        $data = Menu::find($id);
        return view('admin.admin_menu.edit',['list'=>$menuList,'data'=>$data]);

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
//        dd($request->all());

        $model = Menu::find($id);
        $res = $model->update($request->except('_token'));
        if($res){
            return ['status'=>200,'msg'=>'成功'];
        }else{
            return ['status'=>-1,'msg'=>'失败'];
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //

        $menu = Menu::where('pid',$id)->first();
        if(!$menu){
            $res = Menu::destroy($id);
            if($res){
                return 1;
            }else{
                return 0;
            }
        }else{
           return 0;
        }

    }


    public function getMenu($id)
    {
        $menu = Menu::get(['id','name','pid']);
        $auth_list = $menu; // 获取数据库中 权限表数据
        if($id==0){
            $role_auth_list = []; // 获取数据库中某个角色的 角色-权限表数据
        }else{
            $checkedMenu = Role::where('id',$id)->value('menus');
            foreach ($checkedMenu as $k=>$v){
                $role_auth_list[$k]['authid'] = (int)$v;
            }
        }
        $data = [
            'code' => 0,
            'msg'  => '获取成功',
            'data' => [
                'list' => $auth_list,
                'checkedId' => array_column($role_auth_list, 'authid'),
            ],
        ];
        return $data;
    }

}
